﻿using System;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using IndexMarkSilverlightClient.Classes;
using System.Collections.ObjectModel;
using IndexMarkSilverlightClient.IndexMarkServiceReference;
using System.Reflection;
using SharedCode;
using System.Diagnostics;
using System.Collections.Generic;

namespace IndexMarkSilverlightClient
{
    public class MainViewModel : ViewModelBase
    {
        private IndexMarkServiceClient client = null;

        public event EventHandler UserLoaded;

        private User user;

        public User User
        {
            get
            {
                return user;
            }
            set
            {
                user = value;
                PropertyChangedInit("User");
            }
        }

        private ObservableCollection<IndexMarkServiceReference.Term> terms = 
            new ObservableCollection<Term>();

        public ObservableCollection<Term> Terms
        {
            get
            {
                return terms;
            }
        }

        public List<Term> TermsOrg
        {
            get
            {
                return terms.Where(x => 
                    x.IDOrganization == currentOrg.IDOrganization).ToList();
            }
        }

        private ObservableCollection<IndexMarkServiceReference.Period> periods = 
            new ObservableCollection<Period>();

        public ObservableCollection<Period> Periods
        {
            get
            {
                return periods;
            }
        }

        private ObservableCollection<IndexMarkServiceReference.Organization> organizations = 
            new ObservableCollection<Organization>();

        public ObservableCollection<Organization> Organizations
        {
            get
            {
                return organizations;
            }
        }

        private Organization currentOrg = null;
        public Organization CurrentOrg 
        {
            get
            {
                return currentOrg;
            }
            set
            {
                if (currentOrg == value) return;
                currentOrg = value;
                PropertyChangedInit("TermsOrg");
                PropertyChangedInit("CurrentOrg");
                PropertyChangedInit("HasEditDate");
            }
        }

        private Term currentTerm = null;

        public Term CurrentTerm
        {
            get
            {
                return currentTerm;
            }
            set
            {
                if (currentTerm == value) return;
                currentTerm = value;
                PropertyChangedInit("CurrentTerm");
                PropertyChangedInit("HasEditDate");
            }
        }

        public bool HasEditDate
        {
            get
            {
                if (currentTerm != null)
                    return currentTerm.StartFill <= DateTime.Now 
                        && currentTerm.StopFill >= DateTime.Now;
                return false;
            }

        }

        public static string Version
        {
            get
            {
                Assembly assembly = Assembly.GetExecutingAssembly();
                AssemblyName name = new AssemblyName(assembly.FullName);
                return name.Version.ToString();
            }
        }

        public bool IsSuperivsor
        {
            get
            {
                if (User != null)
                {
                    return User.IsSupervisor;
                }
                return false;
            }
        }

        public delegate void SelectTermEventHandler(object sender, SelectTermEventsArgs e);

        public event SelectTermEventHandler SelectTerm;

        public void SelectTermInit()
        {
            if (SelectTerm != null)
            {
                //MessageBox.Show(User.Permissions.First().Char.Name);
                //MessageBox.Show(CurrentTerm==null?"---":CurrentTerm.IDTerm.ToString());
                SelectTerm(this, new SelectTermEventsArgs(CurrentTerm, User));
            }
        }

        public MainViewModel()
        {
            client = new IndexMarkServiceClient();
            client.GetPeriodsCompleted += client_GetPeriodsCompleted;
            client.GetOrganizationsCompleted += client_GetOrganizationsCompleted;
            client.GetTermsCompleted += client_GetTermsCompleted; 
            client.GetUserCompleted += client_GetUserCompleted;
        }

        void client_GetPeriodsCompleted(object sender, GetPeriodsCompletedEventArgs e)
        {
            periods = e.Result;
            PropertyChangedInit("Periods");
            LoadCompletedInit(typeof(Period));
        }

        void client_GetOrganizationsCompleted(object sender, GetOrganizationsCompletedEventArgs e)
        {
            organizations = e.Result;
            PropertyChangedInit("Organizations");
            LoadCompletedInit(typeof(Organization));

            LoadTerms();
        }

        void client_GetTermsCompleted(object sender, GetTermsCompletedEventArgs e)
        {
            terms = e.Result;
            PropertyChangedInit("Terms");
            LoadCompletedInit(typeof(Term));
            
            LoadUser();
        }

        void client_GetUserCompleted(object sender, GetUserCompletedEventArgs e)
        {
            User = e.Result;
            LoadCompletedInit(typeof(User));
            if (UserLoaded != null)
                UserLoaded(this, EventArgs.Empty);
        }

        public void LoadTerms()
        {
            client.GetTermsAsync();
        }

        public void LoadOrganization()
        {
            client.GetOrganizationsAsync();
            client.GetPeriodsAsync();
        }

        internal void LoadUser()
        {
            //MessageBox.Show(CurrentTerm == null ? "0*" : CurrentTerm.IDTerm+"*");
            if (this.currentTerm == null)
            {
                //MessageBox.Show("Для текущего отчётного периода права пользователя не установлены");
                //if (!System.Diagnostics.Debugger.IsAttached) return;
                //currentTerm = terms.Single(x => x.IDTerm == 10);
            }
            client.GetUserAsync(this.currentTerm.IDTerm);
        }
    }
}